import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router'
import qs from 'querystring'
import Cookies from 'js-cookie'
// create an axios instance
const service = axios.create({
  baseURL: 'http://localhost:8080',
  // baseURL:'http://127.0.0.1:8888',
  timeout: 5000 // request timeout
})

// response interceptor
service.interceptors.response.use(
  response => {
    // res就是后端返回来的结果， { status,message,data,timestamp}
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 200) {
      // 消息弹框
      Message({ message: res.msg, type: 'error', duration: 5 * 1000 })
      if (res.code === 401) {
        logout()
      }
      // 返回承诺失败对象
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.msg,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
export function get(url, params) {
  return service.get(url, {
    params, // get 请求时带的参数
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest'
    }
  })
}
export function del(url, params) {
  return service.delete(url, {
    params, // get 请求时带的参数
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest'
    }
  })
}

// 自定义post
export function post(url, data) {
  return service.post(url, qs.stringify(data), {
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
  })
}


// 获取验证码
export function getCodeImg() {
  return service.request({
    url: '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

// 注册
export function register(data) {
  return service.request({
    url: '/register',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}
// 删除投诉建议
export function delSuggest(id) {
    return service.request({
        url: '/system/suggest/' + id,
        method: 'delete',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 删除购物车
export function delShopping(id) {
    return service.request({
        url: '/system/shopping/' + id,
        method: 'delete',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 修改房源订购
export function updateOrder(data) {
    return service.request({
        url: '/system/order',
        method: 'put',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 删除申请看房记录
export function delRecord(id) {
    return service.request({
        url: '/system/record/' + id,
        method: 'delete',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 登录方法
export function login(data) {
  return service.request({
    url: '/login',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 获取用户详细信息
export function getInfo() {
  return service.request({
    url: '/getInfo',
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return service.request({
    url: '/system/user/profile',
    method: 'put',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询轮播图列表
export function listSlideshow(query) {
    return service.request({
        url: '/system/slideshow/list',
        method: 'get',
        params: query
    })
}
// 新增景点评价
export function addEvaluate(score,content,sid,scenic) {
    const data = {
        score,content,sid,scenic
    }
    return service.request({
        url: '/system/evaluate',
        method: 'post',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  }
  return service.request({
    url: '/system/user/profile/updatePwd',
    method: 'put',
    params: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询用户个人信息
export function getUserProfile() {
  return service.request({
    url: '/system/user/profile',
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询房源详细
export function getHouseDetail(id) {
    return service.request({
        url: '/system/house/' + id,
        method: 'get'
    })
}

// 查询信息详细
export function getInfoDetail(title) {
    return service.request({
        url: '/system/text/title',
        method: 'get',
        params:{
            title:title
        }
    })
}

// 查询全部房源列表
export function getHouse(query) {
    return service.request({
        url: '/system/house/list',
        method: 'get',
        params: query
    })

}
// 新增申请看房记录
export function addRecord(data) {
    return service.request({
        url: '/system/record',
        method: 'post',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken()
        }
    })
}
// 新增购物车
export function addShopping(data) {
    return service.request({
        url: '/system/shopping',
        method: 'post',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken()
        }
    })
}
// 新增房源订购
export function addOrder(data) {
    return service.request({
        url: '/system/order',
        method: 'post',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken()
        }
    })
}

// 查询投诉建议列表
export function suggestInfo() {
    return service.request({
        url: '/system/suggest/my',
        method: 'get',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 查询订购列表
export function orderInfo() {
    return service.request({
        url: '/system/order/myOrder',
        method: 'get',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 新增投诉建议
export function addSuggest(data) {
    return service.request({
        url: '/system/suggest',
        method: 'post',
        data: data,
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 查询看房列表
export function recordInfo() {
    return service.request({
        url: '/system/record/myRecord',
        method: 'get',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
// 查询购物车列表
export function shoppingInfo() {
    return service.request({
        url: '/system/shopping/myShop',
        method: 'get',
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}
const TokenKey = 'Portal-Token'
export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}
// 退出
async function logout() {
  router.push(`/login`)
}

export default service
